home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 14992 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.3 KB  |  86 lines

  1. Newsgroups: comp.lang.c
  2. Path: twisto.eng.hou.compaq.com!news
  3. From: Cindy McGee <cindym@bangate.compaq.com>
  4. Subject: Q:  ifstream/memory problem?
  5. Message-ID: <3173C9D4.7480@bangate.compaq.com>
  6. Sender: news@twisto.eng.hou.compaq.com (System Administrator)
  7. Mime-Version: 1.0
  8. X-Mailer: Mozilla 2.0 (Win95; I)
  9. Content-Type: text/plain; charset=us-ascii
  10. Organization: Compaq Computer Corporation
  11. Date: Tue, 16 Apr 1996 16:24:52 GMT
  12. X-Nntp-Posting-Host: 172.18.220.53
  13. Content-Transfer-Encoding: 7bit
  14.  
  15. I may have an ifstream problem, a memory problem, or a combination thereof, and my reference 
  16. material isn't detailed enough on iostream behavior.  I've outlined the problem below.  Any ideas?
  17.  
  18. In the constructor of a large object I am instantiating a list of smaller objects whose member 
  19. variables are filled with information read from a text file.  Later, when accessing the larger 
  20. object via member functions, the object exhibits strange behavior.  Inside of 
  21. TheApp->pLargeOb->Foo(), watching TheApp->pLargeOb->m_iXxx gives a value of 1 but this->m_iXxx is 
  22. uninitialized.
  23.  
  24. Running Bounds Checker on the app shows several errors.  I get an invalid argument error in the 
  25. streambuf's destructor on a delete statement, a memory overrun in the filebuf's scalar deleting 
  26. destructor, and another invalid argument error in the filebuf's scalar deleting destructor.  
  27. Finally, I get a dynamic memory overrun where the large object is instantiated via new.
  28.  
  29. A code snippet is below.
  30.  
  31. (I hope this posts -- it's my second attempt!)
  32.  
  33.  
  34. Thanks,
  35.  
  36. Cindy McGee
  37. -----------------------
  38. cindym@bangate.compaq.com
  39.  
  40. ===========================================================
  41. LargeOb::LargeOb()
  42. {
  43.    ...
  44.    m_pListSmall = new SmallList("data.txt");
  45.    ...
  46. }
  47.  
  48. void SmallList::Foo(char* szFileName)
  49. {
  50.    char sz[255];
  51.    ...
  52.    ifstream file(szFileName);
  53.    
  54.    if (!file)
  55.     ... // error handling
  56.    else
  57.    {
  58.     int iNumElems = 0;
  59.     file >> iNumElems;
  60.     ...
  61.     
  62.     file >> sz;
  63.     for (int i = 0; i < iNumElems; i++)
  64.     {
  65.        pNewElem = new Elem();
  66.        if ('*' == sz[0])
  67.        {
  68.         file.ignore(1000,'/');
  69.         file >> sz;
  70.        }
  71.     
  72.        if (0 == strcmp(sz,"Field1"))
  73.        {
  74.         file.eatwhite();
  75.         file.getline(sz,255);
  76.         pNewElem->SetField1(sz);
  77.         file >> sz;
  78.        }
  79.  
  80.        .. // many more string compares, sets, and extractions
  81.  
  82.        Append(*pNewElem);        // ptr copy only
  83.     }
  84.    }
  85. }
  86.